package com.shujia.sql

import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

object Demo1sql {
  def main(args: Array[String]): Unit = {


    val conf = new SparkConf().setMaster("local").setAppName("Demo1sql")

    val sc = new SparkContext(conf)


    //spark  sql 入口
    val sqlContext = new SQLContext(sc)

    //读取json数据创建df
    val df: DataFrame = sqlContext.read.json("spark/data/student.json")


    //打印列的类型
    df.printSchema()

    //相当于action算子
    df.show()


    df.foreach(row => {

      val name = row.getAs[String]("name")
      val age = row.getAs[Long]("age")
      val clazz = row.getAs[String]("clazz")

      println(s"$name\t$age\t$clazz")
    })


    df.select("name").show()

    df.select(df("name"), df("age") + 1).show()


    df.where("age > 23").show()

    df.groupBy("age").count().show()


    /**
      * 写sql
      *
      * 需要将DF注册成一张表
      *
      */

    df.registerTempTable("student")


    sqlContext.sql("select age ,count(1) from student group by age").show()

  }
}
